<#import "/spring.ftl" as spring />
<#import "/inspector-time-macros.ftl" as imacro />
<html>
<head>
</head>

<body>

<link rel="stylesheet" type="text/css" href="${springMacroRequestContext.contextPath}/css/reports.css" />

<!-- Pop up calendar -->
<link rel="styleSheet" href="${springMacroRequestContext.contextPath}/css/calendarPopup.css" type="text/css">
<script src="${springMacroRequestContext.contextPath}/js/CalendarPopup.js"></script>
<script src="${springMacroRequestContext.contextPath}/js/CalendarPopup-ext.js"></script>

<div id="content">

	<h1>ACTIVITY REPORT</h1>
	
	<div class="reportSearch">
	
		<p>To view a report of your inspector activity, please enter a start and end date in format MM/dd/yyyy<#if inspectorCompany.inspectors?size gt 1>, select the desired inspector, </#if>and click "GET REPORT".</p>
		
		<@imacro.listErrors "command" />
		
		<form method="post" name="searchForm">
		
		<#if inspectorCompany.inspectors?size gt 1>
				<div class="formElement"><label>VIEW ACTIVITY FOR:</label>
					<@imacro.bind "command.inspectorId" />
		    		<select id="${imacro.status.expression}" name="${imacro.status.expression}">
		    			<option value=""<@imacro.checkSelected ""/>>Entire company</option>
				    	<#list inspectorCompany.inspectors as inspector>
			    			<option value="${inspector.id}"<@imacro.checkSelected inspector.id/>>${inspector.name}</option>
			        	</#list>
					</select>
				</div>
			</#if>
		
			<div class="formElemRow"></div>
			
			<div class="formElement"><label>START DATE</label>
				<div class="calendarWidget">
					<@spring.formInput "command.startDate" />
					<script>writeCalLink('document.searchForm.startDate', 'anchor1')</script>
				</div>
			</div>
			<div class="formElement"><label>END DATE</label>
				<div class="calendarWidget">
					<@spring.formInput "command.endDate" />
					<script>writeCalLink('document.searchForm.endDate', 'anchor2')</script>
				</div>
			</div>
			
			<br style="clear:both" />
			<input type="submit" value="GET REPORT" class="formButton"/>
		</form>
		
	</div>
	
	
	<#if inspections??>
	
		<#if inspections?has_content>
		
			<div class="printReportLink"><a href="javascript:window.print()">print report</a></div>
		
			<b>ACTIVITY FOR <#if selectedInspector??>${selectedInspector.name?upper_case}<#else>${inspectorCompany.name?upper_case}</#if> 
				(${command.startDate.time?string("MM/dd/yyyy")} - ${command.endDate.time?string("MM/dd/yyyy")})</b>
		
			<#assign totalPrice = 0.0 />
			
			<table class="report">
				<tr>
					<th>DATE</th>
					<th>CUSTOMER</th>
					<th>ADDRESS</th>
					<th>INSPECTOR</th>
					<th class="currency">FEE</th>
				</tr>
				
				<#list inspections?sort_by("inspectionTime")?reverse as inspection>
				
					<#if inspection.inspectionStatus != 'CANCELLED'>
				
						<#assign totalPrice = (totalPrice + inspection.finalInspectionFee) />
					
						<tr>
							<td>${inspection.inspectionTime?string("MM/dd/yyyy")}</td>
							<td>${inspection.homeBuyer.name}</td>
							<td>${inspection.property.location.street1}, ${inspection.property.location.city}, ${inspection.property.location.state}, ${inspection.property.location.zipCode}</td>
							<td>${inspection.inspector.name}</td>
							<td class="currency">${inspection.finalInspectionFee?string.currency}</td>
						</tr>
					</#if>
				</#list>
					<tr>
						<td colspan="4" class="total">TOTAL:</td>
						<td class="total">${(totalPrice?string.currency)!}</td>				
					</tr>
			</table>
			
			<#if !selectedInspector?? && inspectorCompany.inspectors?size gt 1>
				<h2>TOTALS PER INSPECTOR:</h2>
				<table>
					<#list inspectorCompany.inspectors as inspector>
						<tr>
							<td style="padding: 2px">${inspector.name}</td>
							<td style="padding: 2px">
								<#assign totalForInspector = 0.0 />
								<#list inspections as inspection>
									<#if (inspection.inspector == inspector) && (inspection.inspectionStatus != 'CANCELLED')>
										<#assign totalForInspector = (totalForInspector + inspection.finalInspectionFee) />
									</#if>							
								</#list>
								
								${totalForInspector?string.currency}
							</td>
						</tr>
					</#list>
				</table>
			</#if>
			 
		<#else>
			<div class="noResults">No activity within this time frame.</div>
		</#if>
			
	</#if>
</div>

</body>
</html>